package cn.sinata.wanglin.utils;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.text.TextUtils;

import com.hyphenate.util.HanziToPinyin;

import cn.sinata.wanglin.model.PhoneContact;

import java.util.ArrayList;

/**
 *
 * Created by liaoxiang on 16/12/20.
 */

public class ContactUtils {

    private static String[] projection_getContractList = {
            ContactsContract.Data.RAW_CONTACT_ID,
            ContactsContract.Data.MIMETYPE, ContactsContract.Data.DATA1,
            ContactsContract.Data.DATA2, ContactsContract.Data.DATA3,
            ContactsContract.RawContacts.ACCOUNT_TYPE,
            ContactsContract.Data._ID, ContactsContract.Data.TIMES_CONTACTED,
            ContactsContract.Data.DATA5, ContactsContract.Data.DATA6,};

    public static ArrayList<PhoneContact> getPhoneContacts(Context mContext,boolean showSim){
        ArrayList<PhoneContact> contacts = new ArrayList<>();
        String where = "(" + ContactsContract.Data.MIMETYPE + " = ? or "
                + ContactsContract.Data.MIMETYPE + " = ? or "
                + ContactsContract.Data.MIMETYPE + " = ? or "
                + ContactsContract.Data.MIMETYPE + " = ? or "
                + ContactsContract.Data.MIMETYPE + " = ? or "
                + ContactsContract.Data.MIMETYPE + " = ?)";
        if (!showSim) {
            where += " AND (" + ContactsContract.RawContacts.ACCOUNT_TYPE
                    + " NOT LIKE " + "'%sim%'" + " or "
                    + ContactsContract.RawContacts.ACCOUNT_TYPE + " is null)";
        }
        Cursor cursor = null;
        String[] WhereParams = {
                ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
                ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE,
                ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE,
                ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE,
                ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE,
                ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE};
        Uri uri = ContactsContract.Data.CONTENT_URI;
        try {
            cursor =  mContext.getContentResolver().query(uri, projection_getContractList, where,
                    WhereParams, ContactsContract.Data.DISPLAY_NAME + ","
                            + ContactsContract.Data.RAW_CONTACT_ID);
            if (cursor != null) {
                PhoneContact cli = null;

                long tmpid = -1;
                while (cursor.moveToNext()) {
                    long rawContactId = cursor.getLong(0);
                    String mimetype = cursor.getString(1);
                    String data1 = cursor.getString(2);
                    if (tmpid != rawContactId) {
                        if (cursor.getPosition() != 0 && !TextUtils.isEmpty(cli.getContactId())) {
                            if(TextUtils.isEmpty(cli.getName())) {
                                cli.setName(cli.getContactId());
                            }
                            contacts.add(cli);
                        }
                        cli = new PhoneContact();
                        cli.setId(rawContactId);
                    }
                    tmpid = rawContactId;
                    if (mimetype.equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
                        cli.setContactId(data1);
                    }
                    if (mimetype.equals(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)) {
                        cli.setName(data1);
                    }
                    String letter = formatNamePy(cli.getName());
                    cli.setLetter(letter);
                    // 最后一条记录时添加最后一个
                    if (cursor.getPosition() == (cursor.getCount() - 1)) {
                        if(TextUtils.isEmpty(cli.getName())) {
                            cli.setName(cli.getContactId());
                        }
                        contacts.add(cli);
                    }
                }
                cursor.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return contacts;
    }

    public static String formatNamePy(String name){
        final String DefaultLetter = "#";
        if (TextUtils.isEmpty(name)) {
            return DefaultLetter;
        }
        char char0 = name.toLowerCase().charAt(0);
        if (Character.isDigit(char0)) {
            return DefaultLetter;
        }
        ArrayList<HanziToPinyin.Token> l = HanziToPinyin.getInstance().get(name.substring(0, 1));
        if (l != null && l.size() > 0 && l.get(0).target.length() > 0)
        {
            HanziToPinyin.Token token = l.get(0);
            String letter = token.target.substring(0, 1).toUpperCase();
            char c = letter.charAt(0);
            if (c < 'A' || c > 'Z') {
                return DefaultLetter;
            }
            return letter;
        }
        return DefaultLetter;
    }
}
